package haha.life.common;



/**
 * Description:
 *
 * @author cy
 * @date: 2020-12-11 10:02
 * @Version 1.0
 */
public class BelongToSqlUtils {

    /**
     * 获取belongTo语句的sql
     *
     * @param arr belongTo所在数组
     * @param name 拼接字段名
     * @return
     */
    public static String getBelongToSql(String[] arr, String name) {

        if (arr == null || arr.length == 0) {
            return "";
        }
        int arrMaxLength=arr[0].length();
        //获取数组里 最长数据的长度
        for (int i=0;i<arr.length;i++){
            if (arr[i].length()>arrMaxLength) {
                arrMaxLength=arr[i].length();
            }
        }
        //belongTo括号内的个数  包含了;
        int num=243/(arrMaxLength+1);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            //首次
            if (i == 0) {
                buffer.append(name).append(" BELONG_TO('");
            }
            //多少个一组
            if (i != 0 && i % num == 0) {
                if (buffer.length() !=0) {
                    buffer.deleteCharAt(buffer.length() - 1);
                }
                //  加随机数
                String randomString = RandomStringUtils.getRandomString(7);
                buffer.append(",").append(randomString);

                buffer.append("') or ").append(name).append(" BELONG_TO('");
            }
            buffer.append(arr[i]).append(",");
        }
        //  加随机数
        String randomString = RandomStringUtils.getRandomString(7);
        buffer.append(randomString).append(",");

        buffer.deleteCharAt(buffer.length() - 1).append("')");
        return buffer.toString();
    }

    /**
     * 获取NotBelongTo语句的sql
     *
     * @param arr belongTo所在数组
     * @param name 拼接字段名
     * @return
     */
    public static String getNotBelongToSql(String[] arr, String name) {
        if (arr == null || arr.length == 0) {
            return "";
        }
        int arrMaxLength=arr[0].length();
        //获取数组里 最长数据的长度
        for (int i=0;i<arr.length;i++){
            if (arr[i].length()>arrMaxLength) {
                arrMaxLength=arr[i].length();
            }
        }
        //belongTo括号内的个数  包含了;
        int num=243/(arrMaxLength+1);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            //首次
            if (i == 0) {
                buffer.append(" NOT ").append(name).append(" BELONG_TO('");
            }
            //多少个一组
            if (i != 0 && i % num == 0) {
//            if (buffer.length()>17 && buffer.length()%220<200) {
                if (buffer.length() !=0) {
                    buffer.deleteCharAt(buffer.length() - 1);
                }
                //  加随机数
                String randomString = RandomStringUtils.getRandomString(7);
                buffer.append(",").append(randomString);

                buffer.append("') AND ").append("NOT ").append(name).append(" BELONG_TO('");
            }
            buffer.append(arr[i]).append(",");
        }
        //  加随机数
        String randomString = RandomStringUtils.getRandomString(7);
        buffer.append(randomString).append(",");

        buffer.deleteCharAt(buffer.length() - 1).append("')");
        return buffer.toString();
    }
}
